<template>
  <div class="form-wrapper">
    <header class="form-header">
      <!-- 如果需要使用图标或其他组件，这里自行替换 -->
      <BaseButton @click="goBack">
        <ChevronsLeft/>
      </BaseButton>
      <div style="width: 24px"></div>
    </header>

    <form class="form-container" @submit.prevent="updateGender">
      <h1 class="page-title">修改性别</h1>
      <div class="form-group gender-options">
        <div
          class="gender-option"
          :class="{ active: gender === '男' }"
          @click="gender = '男'"
        >
          男
        </div>
        <div
          class="gender-option"
          :class="{ active: gender === '女' }"
          @click="gender = '女'"
        >
          女
        </div>
      </div>

      <button type="submit" class="submit-button">
        保存修改
      </button>
    </form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ChevronsLeft } from 'lucide-vue-next';
import BaseButton from '@/components/BaseButton.vue';
import basicDetailIcon from '@/assets/icons/basic-detail.svg'
import axios from 'axios'

const router = useRouter()
const gender = ref('男') // 默认值设为"男"
const icons = { basicDetail: basicDetailIcon }

const goBack = () => router.go(-1)

const updateGender = async () => {
  const currentUser = JSON.parse(localStorage.getItem('user'));
  const userId = currentUser ? currentUser.uid : null;

  if (!userId) {
    alert('未找到当前用户，请重新登录');
    return;
  }

  try {
    const response = await axios.put('/api/auth/information/sex',
      {
        uid: userId,
        sex: gender.value
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': localStorage.getItem('token')
        }
      }
    );

    if (response.data.code === 0) {
      // 更新本地存储的用户信息
      currentUser.sex = gender.value;
      localStorage.setItem('user', JSON.stringify(currentUser));
      
      alert('性别修改成功');
      router.go(-1);
    } else {
      throw new Error(response.data.message || '修改失败');
    }
  } catch (error) {
    console.error('修改错误:', error);
    alert(error.response?.data?.message || error.message || '修改失败');
  }
};
</script>

<style scoped>
.form-wrapper {
  min-height: 100vh;
  background-color: #fff;
  padding: 16px;
}

.form-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.btn-back {
  width: 40px;
  height: 40px;
  /* ...existing code... */
  background: none;
  border: none;
  cursor: pointer;
}

.page-title {
  font-size: var(--font-size-large);
  font-family: var(--font-family-bold);
  color: var(--text-primary);
  margin: 10px 0;
}

.form-container {
  max-width: 600px;
  margin: 0 auto;
  background: #ffffff;
  border-radius: 12px;
  padding: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  font-size: var(--font-size-normal);
  font-weight: 500;
  color: #333333;
  margin-bottom: 8px;
}

.gender-options {
  display: flex;
  gap: 16px;
  /* 或根据需求设置合适的布局 */
}

.gender-option {
  flex: 1;
  text-align: center;
  padding: 12px 0;
  font-size: var(--font-size-normal);
  color: #333333;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  cursor: pointer;
}
.gender-option.active {
  border-color: #4f46e5;
  background-color: rgba(79, 70, 229, 0.1);
}

.submit-button {
  width: 100%;
  padding: 15px;
  background-color: var(--primary-color);
  color: var(--text-light);
  border: none;
  border-radius: var(--border-radius-small);
  margin-top: 20px;
  cursor: pointer;
}

.submit-button:hover {
  background-color: #4338ca;
}

@media (max-width: 640px) {
  .form-container {
    padding: 20px;
  }
}
</style>
